<template>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    confirmBtn="确认推荐"
    @confirm="onSubmit"
    :confirmBtn="{
        content: loading?'提交中...':'确认',
        theme: 'primary',
        loading: loading,
      }"
    width="80%"
    top="5%"
    v-if="editOpen"
  >
    <div style="height: 65vh">
      <innovation-detail :header="dialog.header"  :recommend-open="true" :objId="objId" :visible.sync="dialog.open_innovation_detail" v-if="dialog.open_innovation_detail"></innovation-detail>
      <t-drawer
        :visible="drawerVisible"
        showInAttachedElement
        :showOverlay="false"
        size="27%"
        :preventScrollThrough="false"
        header="推荐说明信息"
      >
                    <t-form labelAlign="top" :data="formData" ref="formObj" labelWidth="180px" :rules="rules">
<!--                      <t-form-item  label="剩余推荐次数(同一成果不重复消耗次数)" name="count">-->
<!--                        <t-input v-model="formData.count" disabled></t-input>-->
<!--                      </t-form-item>-->
                      <t-form-item name="content" label="推荐说明">
                        <t-textarea  :autosize="{minRows: 5}"
                                     v-model="formData.content" placeholder="请输入内容"/>

                      </t-form-item>
                      <t-form-item label="其他推荐材料" name="wcxOther">
                        <file-upload v-model="formData.sysAttMainVo.sysAttMainIds"  :fileList="formData.otherFiles" :multiple="true"
                                     placeholder="遴选过程资料。" tips="">
                          <t-button variant="outline">
                            <t-icon name="cloud-upload" slot="icon"/>
                            上传
                          </t-button>
                        </file-upload>
                      </t-form-item>
                      <p style="color:red; font-size:12px">注意:推荐前请先在“审批附件信息“模块中，上传该项目盖章后的申报书和遴选佐证材料。</p>
                    </t-form>
        <template  #footer>
          <t-button theme="default" @click="closeDrawer">关闭推荐界面</t-button>
          <t-button theme="primary" @click="onSubmit()">提交推荐</t-button>
        </template>

      </t-drawer>
    </div>
    <template #footer>
      <t-button theme="default" @click="closeDialog">取消</t-button>
      <t-button theme="primary" @click="drawerVisible = true">打开推荐界面</t-button>
    </template>
  </t-dialog>
  <t-dialog
    :header="header"
    :visible.sync="visible"
    :onClose="closeDialog"
    :closeOnOverlayClick="false"
    :confirmBtn="null"
    width="80%"
    top="5%"
    v-else
  >
    <div style="height: 65vh">
    <innovation-detail :header="dialog.header"  :objId="objId" :visible.sync="dialog.open_innovation_detail" v-if="dialog.open_innovation_detail"></innovation-detail>
      <t-drawer
        :visible="drawerVisible"
        showInAttachedElement
        :showOverlay="false"
        size="27%"
        :preventScrollThrough="false"
        header="推荐说明信息"
      >
        <t-form labelAlign="top" :disabled="true" :data="formData" ref="formObj" labelWidth="180px" :rules="rules">
<!--          <t-form-item  label="剩余推荐次数(同一成果不重复消耗次数)" name="count">-->
<!--            <t-input v-model="formData.count" disabled></t-input>-->
<!--          </t-form-item>-->
          <t-form-item name="content" label="推荐说明">
            <t-textarea  :autosize="{minRows: 5}"
                         v-model="formData.content" placeholder="请输入内容"/>

          </t-form-item>
          <t-form-item label="其他推荐材料" name="wcxOther">
            <file-upload :disabled="true" v-model="formData.sysAttMainVo.sysAttMainIds"  :fileList="formData.otherFiles" :multiple="true"
                         placeholder="遴选过程资料。" tips="">
              <t-button variant="outline">
                <t-icon name="cloud-upload" slot="icon"/>
                上传
              </t-button>
            </file-upload>
          </t-form-item>
        </t-form>
        <template  #footer>
          <t-button theme="default" @click="closeDrawer">关闭推荐界面</t-button>
<!--          <t-button theme="primary" @click="onSubmit()">提交推荐</t-button>-->
        </template>

      </t-drawer>
      <t-drawer
        :visible="backDrawerVisible"
        showInAttachedElement
        :showOverlay="false"
        size="27%"
        :preventScrollThrough="false"
        header="退回说明信息"
      >
        <t-form  ref="form" :colon="true" label-align="top" :data="formData2" :rules="rules2">
          <t-form-item  name="opinion" label="退回意见">
            <t-textarea  :autosize="{minRows: 5}"
                         v-model="formData2.opinion"  placeholder="请输入内容"/>
          </t-form-item>
        </t-form>
        <template  #footer>
          <t-button theme="default" @click="backDrawerVisible=false">关闭退回界面</t-button>
          <t-button theme="primary" @click="onSubmit2()">提交退回</t-button>
        </template>

      </t-drawer>
<!--    <t-row style="align-items: stretch" >-->
<!--      <t-col :span="12">-->
<!--        <t-card class="step-card" size="small" title="推荐说明信息">-->
<!--          <t-form labelAlign="top" :data="formData" ref="formObj" labelWidth="180px" >-->
<!--&lt;!&ndash;            <t-form-item  label="剩余推荐次数(同一成果不重复消耗次数)" name="count">&ndash;&gt;-->
<!--&lt;!&ndash;              {{formData.count}}&ndash;&gt;-->
<!--&lt;!&ndash;            </t-form-item>&ndash;&gt;-->
<!--            <t-form-item name="content" label="推荐说明">-->
<!--              {{formData.content}}-->
<!--            </t-form-item>-->
<!--            <t-form-item label="其他推荐材料"  name="sysAttMainVo.sysAttMainIds">-->
<!--              <a  class="t-button-link" target="_blank" v-for="(item,index) in formData.otherFiles" :href="item.url" :key="index">-->
<!--                {{item.name}}-->
<!--              </a>-->
<!--            </t-form-item>-->
<!--          </t-form>-->
<!--        </t-card>-->
<!--      </t-col>-->
<!--    </t-row>-->
    </div>
    <template #footer>
      <t-button theme="default" @click="closeDialog">取消</t-button>
      <t-button theme="primary" @click="drawerVisible = true">打开推荐界面</t-button>
      <t-button theme="danger" v-if="backOpen" @click="backDrawerVisible=true">退回</t-button>
    </template>
  </t-dialog>
</template>

<script>
import InnovationDetailDialog from "../../innovation/components/innovation-detail-dialog.vue";
import {getFiles} from "@/api/common";
import FileUpload from "@/components/FileUpload/index.vue";
import InnovationDetail from "../../innovation/components/innovation-detail.vue";


export default {
  name: "recommend-detail-dialog",
  components:{
    InnovationDetailDialog,
    FileUpload,
    InnovationDetail
  },
  props:{
    value:Array,
    objId: [String, Number],
    header: {
      type: String,
      default: '',
    },
    editOpen: {
      type: Boolean,
      default: false
    },
    backOpen: {
      type: Boolean,
      default: false
    }
  },
  data(){
    return {
      drawerVisible: false,
      backDrawerVisible: false,
      visible: true,
      loading: false,
      dialog:{
        open_innovation_detail: true,
        objId: 0,
        header: '',
      },
      formData:{
        count:0,
        content: '',
        innovationId: this.objId,
        sysAttMainVo:{
          sysAttMainIds:[]
        },
        otherFiles:[]
      },
      rules:{
        content: [
          {required: true, message: "推荐说明不能为空"}
        ],
        // count:[
        //   {
        //     required: true,
        //   },
        //   { validator: (val) => val>0, message: `剩余推荐次数需要大于0`, type: "error"}
        // ]
      },
      formData2:{
        businessId: this.objId,
        opinion: '',
        approveStatus: "4",
      },
      rules2: {
        opinion: [
          {required: true, message: "退回意见不能为空"}
        ],
      },
    }
  },
  watch: {
    objId: {
      immediate: true,
      handler(val) {
        if (val) {
          this.getObj(val);
        }
      }
    },
  },
  created() {
    // 获取推荐次数
    // this.$api.wcx.apply.getRemainingQuota(this.$store.state.user.deptId).then(res => {
    //   this.formData.count = res.data.remainData;
    // }).catch((e) => {
    //   this.$message.error(e.toString());
    // });
  },
  methods: {
    getObj() {
      // 获取详情
      this.$api.wcx.apply.recommendInfoGetByInnovationId(this.objId).then(response => {
        // 获取附件
        if(response.data){
          getFiles(response.data.id).then((res) => {
            this.formData.otherFiles = []
            const ids = [];
            if (res.data && res.data.length) {
              res.data.forEach((item) => {
                ids.push(item.id);
                this.formData.otherFiles.push({ name: item.fileName, url: item.fullFilePath, id: item.id, filePath: item.filePath })
              })
              this.formData.sysAttMainVo.sysAttMainIds = ids;
            }
          }).catch((e) => {
            this.$message.error(e.toString());
          });
          this.formData.innovationId = response.data.innovationId;
          this.formData.content = response.data.content;
        }
      }).catch((e) => {
        this.$message.error(e.toString());
      });
    },
    closeDrawer(){
      this.drawerVisible = false
    },
    closeDialog() {
      this.$emit("update:visible", false);
    },
    onSubmit() {
      this.$refs.formObj.validate().then((validateResult) => {
        if (validateResult === true) {
          this.loading = true;
          if(this.formData.id){
            this.$api.wcx.apply.recommendInfoUpdate(this.formData).then(response => {
              this.$message.success("推荐更新成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }else {
            this.$api.wcx.apply.recommendInfoAdd(this.formData).then(response => {
              this.$message.success("推荐成功");
              this.$emit("reload");
              this.$emit("update:visible", false);
              this.loading = false;
            }).catch((e) => {
              this.loading = false;
              this.$message.error(e.toString());
            });
          }

        } else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      })
    },
    onSubmit2(){
      this.$refs.form.validate().then((validateResult) => {
        if (validateResult === true) {
          const confirmDia = this.$dialog.confirm({
            header: "审批提示",
            body: `是否确认提交？`,
            theme: "warning",
            onConfirm: ({e}) => {
              confirmDia.update({
                confirmBtn: {
                  loading: true,
                },
              })
              this.$api.wcx.approve.reviewApprove(this.formData2).then(response => {
                this.$message.success('提交成功');
                this.$emit("reload");
                this.$emit("update:visible", false);
                confirmDia.destroy();
              }).catch((e) => {
                confirmDia.update({
                  confirmBtn: {
                    loading: false,
                  },
                })
                this.$message.error(e.toString());
              });
            },
          })

        }else {
          const firstError = Object.values(validateResult)[0]?.[0]?.message;
          this.$message.error(firstError);
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.t-row {
  margin-bottom: 14px;
}

.step-card {
  height: 100%;
  margin-bottom: 14px;
}

/deep/ .t-drawer .t-drawer__content-wrapper{
  height: calc(100% - var(--td-comp-size-xxxl));
  margin-top: var(--td-comp-size-xxxl);
}
/deep/ .t-drawer__header{
  display: none;
}

</style>
